import request from '@/utils/request'
import Cookies from 'js-cookie'
import { TokenType } from '@/utils/enum'

class Data {
  static view(url) {
    return new Promise((resolve,reject)=>{
      const xhr = new XMLHttpRequest();
      xhr.open("GET", url, true)
      xhr.responseType = "blob"
      xhr.setRequestHeader('Authorization', 'Bearer ' + Cookies.get(TokenType.USER_TOKEN))
      xhr.onload = ()=>{
        if(xhr.status === 200) {
          resolve(xhr.response)
        } else {
          reject();
        }
      }
      xhr.send();
    })
  }
  static get(url, params = {}) {
    return request({
      url,
      method: 'get',
      params
    })
  }
  static post(url, data) {
    return request({
      url,
      method: 'post',
      data
    })
  }
  static add(url, data) {
    return request({
      url,
      method: 'post',
      data
    })
  }
  static edit(url, data) {
    return request({
      url,
      method: 'put',
      data
    })
  }
  static verify(url, data) {
    return request({
      url,
      method: 'put',
      data
    })
  }
  static batchEdit(url, data) {
    return request({
      url,
      method: 'put',
      data
    })
  }
  static delete(url, array_id = []) {
    return request({
      url,
      method: 'delete',
      data: array_id
    })
  }

  static deleteById(url) {
    return request({
      url,
      method: 'delete'
    })
  }

  static editPut(url){
    return request({
      url,
      method:'put',
    })
  }
  static put(url, array_id = []) {
    return request({
      url,
      method: 'put',
      data: array_id
    })
  }
  static upload(url, data) {
    return request({
      url,
      method: 'post',
      headers: {
        'Content-Type': 'multipart/form-data'
      },
      data
    })
  }

  static downFile(url, params = {}) {
    return request({
      url,
      method: 'get',
      responseType: 'arraybuffer',
      params
    })
  }

  static download(url, params = {}) {
    return request({
      url,
      method: 'get',
      responseType: 'blob',
      params
    })
  }

  static importBatch(url, data) {
    return request({
      url,
      method: 'post',
      responseType: 'blob',
      data
    })
  }

}

export default Data
